package cn.boysoft.core.server;

import java.lang.reflect.Method;

import cn.boysoft.core.convert.ResultConvert;
import cn.boysoft.core.exception.JEEException;
/**
 * 
 * @author 付厚俊
 * CreateTime 2013-4-4 下午5:09:32
 */
public class ServiceInfo {

	private Method method;
	private Class dataClazz;
	private Class rtClass;
	private String serviceName;
	private String serviceDescription;
	private boolean enabled = true;
	private String serviceClassId;
	private String commandId;
	private boolean needValue = true;
	ResultConvert resultConvert;
	public Method getMethod() {
		return method;
	}
	void setMethod(Method method) {
		this.method = method;
	}
	public Class getDataClazz() {
		return dataClazz;
	}
	void setDataClazz(Class dataClazz) {
		this.dataClazz = dataClazz;
	}
	
	
	public String getCommandId() {
		return commandId;
	}
	void setCommandId(String commandId) {
		this.commandId = commandId;
	}
	boolean isEnabled() {
		return enabled;
	}
	void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	
	public String getServiceName() {
		return serviceName;
	}
	void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}
	public String getServiceDescription() {
		return serviceDescription;
	}
	void setServiceDescription(String serviceDescription) {
		this.serviceDescription = serviceDescription;
	}
	public String getServiceClassId() {
		return serviceClassId;
	}
	void setServiceClassId(String serviceClassId) {
		this.serviceClassId = serviceClassId;
	}
	
	public boolean isNeedValue() {
		return needValue;
	}
	void setNeedValue(boolean needValue) {
		this.needValue = needValue;
	}
	
	public Class getRtClass() {
		return rtClass;
	}
	public void setRtClass(Class rtClass) {
		this.rtClass = rtClass;
	}
	public ResultConvert getResultConvert() {
		return resultConvert;
	}
	public void setResultConvert(ResultConvert resultConvert) {
		this.resultConvert = resultConvert;
	}
	public Object invoke(Object target,Object arg){
		Object rt = null;
		try {
			rt = this.method.invoke(target, arg);
			if(this.needValue){
				if(this.resultConvert != null){
					rt = resultConvert.convert(rt);
				}
			}else{
				return null;
			}
			
		} catch (Throwable e) {
			Throwable  throwable = e.getCause();
			if(throwable != null){
				throwable.printStackTrace();
				if(throwable instanceof JEEException){
					throw (JEEException)throwable;
				}else{
					throw new JEEException(JEEException.SYSTEM_ERROR);
				}
			}else{
				e.printStackTrace();
				throw new JEEException(JEEException.SYSTEM_ERROR);
			}
		} 
		return rt;
	}
	public Object invoke(Object target){
		Object rt = null;
		try {
			rt = this.method.invoke(target);
			if(this.needValue){
				if(this.resultConvert != null){
					rt = resultConvert.convert(rt);
				}
			}else{
				return null;
			}
		} catch (Exception e) {
			Throwable  throwable = e.getCause();
			if(throwable != null){
				throwable.printStackTrace();
				if(throwable instanceof JEEException){
					throw (JEEException)throwable;
				}else{
					throw new JEEException(JEEException.SYSTEM_ERROR);
				}
			}else{
				e.printStackTrace();
				throw new JEEException(JEEException.SYSTEM_ERROR);
			}
		} 
		return rt;
	}
}
